<template>
	<view class="gui-padding gui-bg-white" style="width: 100vw; min-height: 100vh;">
		<view
			class="gui-padding gui-margin-top-large gui-color-black gui-border-radius-large gui-bg-gray"
			style="line-height: 60rpx;box-shadow: 3rpx 3rpx 3rpx #ccc;border: solid 1rpx #ccc;">
			<view class="text-center">
				{{info.name}}
			</view>
			<view>
				订单号：{{info.order_no}}
			</view>
			<view>
				订单ID：{{info.order_id}}
			</view>
			<view>
				商户号：{{info.mch_id}}
			</view>
			<view>
				金额：{{info.amount}}
			</view>
			<view>
				支付状态：{{info.status_cn}}({{status}})
			</view>

			<view>
				支付类型：{{pay_type_cn}}
			</view>
			<view class="gui-h4 gui-text-center gui-color-red" style="font-weight: bold;">
				{{msg}}
			</view>
		</view>
		<view class="gui-margin-top-large gui-padding">
			<button :disabled="loading" class="gui-button gui-bg-blue gui-color-white" @click="back">返回</button>
			<!-- <button class="gui-button gui-bg-blue gui-color-white" @click="test" >test</button> -->
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pay_type_cn: '',
				info: {
					status_cn: "支付中..",
					order_no: "",
					order_id: "",
					mch_id: "",
					type: "",
					data: {},
				},
				type: "",
				msg: '',
				status: 0,
				loading: false
			}
		},
		onLoad(opt) {
			console.log("pay_info->opt", JSON.stringify(opt));

			let data = opt.data;
			console.log(data);
			data = JSON.parse(data);
			console.log("pay_info->data", data)
			this.info = data.data;
			this.type = data.type;

			if (this.type == "open_wexin") {
				this.pay_type_cn = "微信支付";
			}
			if (this.type == "open_abc") {
				this.pay_type_cn = "农行app";
			}

			this.info.status_cn = "准备开始支付中.."

		},
		onShow() {
			this.status++;
			let self = this;
			if (this.status > 1) {
				this.info.status_cn = "检查支付状态";
				setTimeout(function() {
					self.checkPayStatus();
				}, 1000);
			} else {
				setTimeout(function() {
					self.loading = true;
					self.pay(self.info);
				}, 1000)
				setTimeout(function() {
					self.loading = false;
				}, 3000);
			}
		},
		methods: {
			back() {
				uni.navigateBack();
			},
			test() {
				this.info.order_id = 148;
				this.info.order_no = "BGH08920241208140304121";
				this.info.open_wexin = "open_wexin"
				this.checkPayStatus();
			},
			checkPayStatus() {
				console.log("checkPayStatus", JSON.stringify(this.info));
				uni.showLoading({
					title: "检查支付状态"
				})
				this.msg = "正在检查支付状态..."
				let self = this;
				this.loading = true;
				this.$boya.NPost("/app/pay/order_check", {
					id: this.info.order_id,
					order_no: this.info.order_no,
					pay_type: this.type
				}, function(res) {
					console.log("order_check", res);
					self.loading = false;
					if (res.code == 200) {

						uni.hideLoading();
						uni.showToast({
							title: "支付成功"
						})
						self.msg = "支付成功";
						self.info.status_cn = "支付成功";
						self.status = 1;
						setTimeout(function() {
							self.back()
						}, 2000);

					} else {
						self.info.status_cn = "支付失败";
						self.status = 3;
					}
				}, function(err) {
					self.loading = false;
					console.log("order_check->err", err)
					self.info.status_cn = "支付失败";
					self.msg = "支付失败";
					self.status = 3;
					uni.showToast({
						title: "支付失败",
						icon: 'error'
					})


				})

			},
			pay(data) {
				console.log("pay", data);
				console.log("type", this.type);
				this.msg = "准备开始支付...";
				if (this.type == "open_wxpay" || this.type == 'wx' || this.type == 'abcBridge') {

					let app_id = data.app_id ?? this.info.app_id;
					let app_path = data.app_path ?? this.info.app_path;
					if (this.type != "abcBridge") {
						app_path += "?order_no=" + this.info.order_no;
					}
					// else{
					// 	app_id="wxcfa496c3eca687e7";
					// 	//app_path="page/containers/payment/query-input/query-input?code="+data.code
					// 	app_path="page/containers/payment/query-input/query-input?code=JF-EPAY2017071204819"
					// 	//app_path="subPackages/PaymentCenterWeixin/page/containers/payment/query-input/query-input?code=JF-EPAY2019062100960"
						
					// }
					console.log("open_wxpay=>appid:" + app_id + "|path=" + app_path);

					//打开小程序,非H5
					// #ifndef H5
					plus.share.getServices(function(s) {
						var shares = {};
						for (var i = 0; i < s.length; i++) {
							var t = s[i];
							console.log(t);
							shares[t.id] = t;
							console.log(t.id);
						}
						console.log("shares", shares);

						var sweixin = shares['weixin'];

						if (sweixin) {
							console.log("打开微信服务");
							console.log("打开小程序,"+app_id+","+app_path);
							sweixin.launchMiniProgram({
								id: app_id,
								path: app_path,
								type:0,
							},function(res){
								console.log("launchMiniProgram->ok,",res);
							},function(err){
								console.log('launchMiniProgram->err,',err);
							})
						} else {
							plus.nativeUI.alert('当前环境不支持微信操作!');
							this.msg = "当前环境不支持微信操作";
						}
						console.log("微信服务列表：" + JSON.stringify(sweixin));
					}, function(e) {
						console.log("获取分享服务列表失败：" + e.message);
					});
					//#endif
				}
				if (this.type == "open_abc" || this.type == "app") {
					// 打开农行app
					console.log("打开农行app", JSON.stringify(data));
					// #ifdef APP-PLUS
					var ABCPay = uni.requireNativePlugin('ABC-Pay');
					var ret = ABCPay.isBankABCAvaiable();
					let token = data.token;

					if (ret == true) {
						//发起支付
						console.log("发起支付abcApp", token);
						ABCPay.startBankABC({
							//appID:"com.example.caller",//第三方APP的应用标识。iOS无此项内容；Android默认为第三方apk的包名，(可选)
							//Method:"pay",//业务类型。目前支持的取值仅为pay，表示是支付交易。默认值pay，(可选)
							CallbackID: "com.boya.app", //支付完成后回调用的id。iOS为第三方app的schema，(iOS必需，Android可选)
							TokenID: token //支付订单号(必需)
						}, function(res) {
							if (res.code == 0) {
								//支付结果
								console.log(res.data);
							} else {
								uni.showModal({
									content: res.data
								});
							}
						});
					} else {
						this.$boya.msg('未安装农行掌银,或版本不支持');
						self.msg = "未安装农行掌银,或版本不支持"
					}
					// #endif
				}
			}
		}
	}
</script>

<style>
	page {
		background-color: #efefef;
	}
</style>